package com.gamex.findit.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gamex.findit.ResultCode;
import com.gamex.findit.internal.DefaultConfig;
import com.gamex.findit.model.Friend;
import com.gamex.findit.service.FriendService;
import com.gamex.findit.util.CommonUtil;
import com.mysql.jdbc.StringUtils;

@Controller("memberFriend")
public class FriendController {
	
	private static final Logger logger = LoggerFactory.getLogger(FriendController.class);
	
	@Autowired FriendService friendService;
	@Autowired DefaultConfig config;
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param friend
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/app/member/friendList")
	public ResponseEntity<String> friendList( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, Model model
							, @ModelAttribute Friend friend) throws Exception{

		HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/json; charset=utf-8");
        
		ModelMap body = new ModelMap();
		List<Friend> result = friendService.getFriend(friend);
		
		if(result != null){
			body.addAttribute(ResultCode.RESULT,ResultCode.RESULT_CODE_SUCCESS);
			body.addAttribute("list", result);
		}else{
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
	return new ResponseEntity<String>(CommonUtil.toJson(body), responseHeaders, HttpStatus.CREATED);
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param friend
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/app/member/friendView")
	public ResponseEntity<String> viewfriend( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, ModelMap model
							, @ModelAttribute Friend friend) throws Exception{
		
		HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/json; charset=utf-8");
        
		ModelMap body = new ModelMap();
		Friend result = null;
		
		try {
			result = friendService.getFriendView(friend);
		} catch (Exception e) {
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
		if(result != null){
			body.addAttribute(ResultCode.RESULT,ResultCode.RESULT_CODE_SUCCESS);
			body.addAttribute("detail", result);
		}else{
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
	return new ResponseEntity<String>(CommonUtil.toJson(body), responseHeaders, HttpStatus.CREATED);
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param friend
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/app/member/addFriend")
	public ResponseEntity<String> addFriend( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, ModelMap model
							, @ModelAttribute Friend friend) throws Exception{
		
		HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/json; charset=utf-8");
        
		ModelMap body = new ModelMap();
		
		try {
			if(!StringUtils.isNullOrEmpty(friend.getUserId()) && !StringUtils.isNullOrEmpty(friend.getFriendId())){
				Friend result = friendService.getFriendView(friend);
				
				if(result == null){
					friendService.insertFriend(friend);
				}else{
					body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
				}
			}else{
				body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
			}
			
			body.addAttribute(ResultCode.RESULT,ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
	return new ResponseEntity<String>(CommonUtil.toJson(body), responseHeaders, HttpStatus.CREATED);
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param friend
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/app/member/deleteFriend")
	public ResponseEntity<String> deleteFriend( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, ModelMap model
							, @ModelAttribute Friend friend) throws Exception{
		
		HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/json; charset=utf-8");
        
		ModelMap body = new ModelMap();
		try {
			if(!StringUtils.isNullOrEmpty(friend.getUserId()) && !StringUtils.isNullOrEmpty(friend.getFriendId())){
				friendService.deleteFriend(friend);
			}else{
				body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
			}
			body.addAttribute(ResultCode.RESULT,ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
	return new ResponseEntity<String>(CommonUtil.toJson(body), responseHeaders, HttpStatus.CREATED);
	}
	
}
